package com.yanqu.road.server.manger.player;

import com.yanqu.road.entity.player.UserBlacklist;
import com.yanqu.road.logic.bussiness.player.BlacklistBussiness;
import com.yanqu.road.server.TempMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BlacklistMgr extends TempMgr {
    private static Map<Long, Map<Integer, Map<Long, UserBlacklist>>> userBlacklistMap;
    private static Map<Long, Map<Integer, Map<Long, Map<Long, UserBlacklist>>>> crossUserBlacklistMap;
    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        userBlacklistMap = BlacklistBussiness.getAllUserBlacklistMap();
        crossUserBlacklistMap = BlacklistBussiness.getAllCrossUserBlacklistMap();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
    * 描述：放置玩家黑名单
    * 作者：zrq
    * 时间：2022/12/30
    * 参数：

    * 返回值：
    **/
    public static void putPlayerBlacklistMap(long userId, Map<Integer, Map<Long, UserBlacklist>> dataMap){
        userBlacklistMap.put(userId, dataMap);
    }

    public static void putPlayerCrossBlacklistMap(long userId, Map<Integer, Map<Long, Map<Long, UserBlacklist>>> dataMap){
        crossUserBlacklistMap.put(userId, dataMap);
    }

    /**
    * 描述：是否在对方黑名单中
    * 作者：zrq
    * 时间：2022/12/30
    * 参数：

    * 返回值：
    **/
    public static boolean isInBlacklist(long userId, int type, long blacklistUserId, long blacklistServerId){
        boolean result = isInLocalBlacklist(userId, type, blacklistUserId);
        if(!result){
            result = isInCrossBlacklist(userId, type, blacklistUserId, blacklistServerId);
        }
        return result;
    }

    /**
    * 描述：是否本地黑名单
    * 作者：zrq
    * 时间：2023/1/3
    * 参数：

    * 返回值：
    **/
    public static boolean isInLocalBlacklist(long userId, int type, long blacklistUserId){
        Map<Integer, Map<Long, UserBlacklist>> typeMap = userBlacklistMap.get(userId);
        if(null == typeMap){
            return false;
        }
        Map<Long, UserBlacklist> dataMap = typeMap.get(type);
        if(null == dataMap){
            return false;
        }
        return isBlacklistMap(blacklistUserId, dataMap);
    }

    /**
    * 描述：是否跨服黑名单
    * 作者：zrq
    * 时间：2023/1/3
    * 参数：

    * 返回值：
    **/
    public static boolean isInCrossBlacklist(long userId, int type, long blacklistUserId, long blacklistServerId){
        Map<Integer, Map<Long, Map<Long, UserBlacklist>>> userMap = crossUserBlacklistMap.get(userId);
        if(null == userMap){
            return false;
        }
        Map<Long, Map<Long, UserBlacklist>> typeMap = userMap.get(type);
        if(null == typeMap){
            return false;
        }
        Map<Long, UserBlacklist> dataMap = typeMap.get(blacklistServerId);
        if(null == dataMap){
            return false;
        }
        return isBlacklistMap(blacklistUserId, dataMap);
    }

    private static boolean isBlacklistMap(long blacklistUserId, Map<Long, UserBlacklist> dataMap){
        UserBlacklist data = dataMap.get(blacklistUserId);
        if(null == data){
            return false;
        }
        return !data.isDelete();
    }

    /**
    * 描述：离线清除一下玩家信息，避免占用内存
    * 作者：zrq
    * 时间：2023/1/4
    * 参数：

    * 返回值：
    **/
    public static void offlineClearCrossBlacklistUserInfo(long userId){
        Map<Integer, Map<Long, Map<Long, UserBlacklist>>> typeMap = crossUserBlacklistMap.get(userId);
        if(null == typeMap){
            return;
        }
        for(Map<Long, Map<Long, UserBlacklist>> serverMap : typeMap.values()){
            for(Map<Long, UserBlacklist> dataMap : serverMap.values()){
                for(UserBlacklist data : dataMap.values()){
                    data.setUserBaseInfo(null);
                }
            }
        }
    }

    public static List<Long> getUserAllBlackList(long userId, int type){
        List<Long> result = new ArrayList<>();
        Map<Integer, Map<Long, UserBlacklist>> typeMap = userBlacklistMap.get(userId);
        if(typeMap != null){
            Map<Long, UserBlacklist> userMap = typeMap.get(type);
            if(userMap != null){
                for (Map.Entry<Long, UserBlacklist> entry : userMap.entrySet()) {
                    UserBlacklist blacklist = entry.getValue();
                    if(!blacklist.isDelete()){
                        result.add(blacklist.getBlacklistUserId());
                    }
                }
            }
        }
        Map<Integer, Map<Long, Map<Long, UserBlacklist>>> crossTypeMap = crossUserBlacklistMap.get(userId);
        if(crossTypeMap != null){
            Map<Long, Map<Long, UserBlacklist>> serverMap = crossTypeMap.get(type);
            if(serverMap != null){
                for (Map.Entry<Long, Map<Long, UserBlacklist>> entry : serverMap.entrySet()) {
                    Map<Long, UserBlacklist> userMap = entry.getValue();
                    for (Map.Entry<Long, UserBlacklist> blacklistEntry : userMap.entrySet()) {
                        UserBlacklist blacklist = blacklistEntry.getValue();
                        if(!blacklist.isDelete()){
                            result.add(blacklist.getBlacklistUserId());
                        }
                    }
                }
            }
        }

        return result;
    }
}
